package com.example.sellersale.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sellersale.entity.Goods;
import com.example.sellersale.mapper.GoodsMapper;
import com.example.sellersale.server.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Goods> scanGoods() {
        List<Goods> goods = goodsMapper.selectList();
        return goods;
    }

    @Override
    public Boolean addGoods(Goods goods) {
        //当库存中有该商品，则改变商品的数量
        //当库存中没有该商品的时候，增加该商品
        String goodsName = goods.getGoodsName();
        Goods goods1 = goodsMapper.selectGoodsByName(goodsName);
        int update = 0;
        if(goods1!=null){
            //更改库存
            Integer goodsNum = goods1.getGoodsNum()+goods.getGoodsNum();
            goods1.setGoodsNum(goodsNum);
            update = goodsMapper.updateById(goods1);
        }else{
            //添加商品
            update = goodsMapper.insert(goods);
        }
        return update==1;
    }

    @Override
    public Goods selectGoodsByName(String name) {
        Goods goods = goodsMapper.selectGoodsByName(name);
        return goods;
    }
}
